from pylib import models
from pylib import web

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app


class ViewPerson(web.BaseHandler):

  canonical_url = '/public/view_person'

  @web.retry
  @web.model_from_request
  def get(self, person):
    identity = None
    if isinstance(person, models.Identity):
      # We were given an Identity instead of a Person
      identity = person
      person = person.person
    identities = dict((x.key(), x) for x in person.identity_set)
    if identity:
      identities[identity.key()] = identity
    changes = [x.change for x in person.changedperson_set]
    changes.sort(key=lambda x: x.changed_at, reverse=True)
    self.render_response('view_person.html',
                         person=person,
                         identities=identities.values(),
                         changes=changes)


application = webapp.WSGIApplication([('.*', ViewPerson)])

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()
